const { ListToNode, NodeToList, ListNode, timeL } = require("../utils.js");
// 25. K 个一组翻转链表
var reverseKGroup = function (head, k) {
  if (k === 1) return head;
  let node = new ListNode(0, head),
    current = node;
  try {
    while (current) {
      let i = 1;
      let startNode = current.next;
      let currentNode = current.next;
      let lastNode = null;
      while (i < k) {
        currentNode = currentNode.next;
        lastNode = currentNode.next;
        startNode = new ListNode(currentNode.val, startNode);
        i++;
      }
      currentNode = current.next;
      currentNode.next = lastNode;
      current.next = startNode;
      current = currentNode;
    }
  } catch (error) {
    return NodeToList(node.next);
  }
  return NodeToList(node.next);
};

let head = ListToNode([1, 2, 3, 4, 5]),
  k = 1;
timeL(reverseKGroup, [head, k]);
